export interface RouteParams {
  path: string;
  query?: any;
}

export function useRouter() {
  const getRoute = (params: RouteParams) => {
    let url = params.path;
    if (params.query) {
      const list = Object.keys(params.query).map(
        (key) => `${key}=${params.query[key]}`
      );
      url = url + '?' + list.join('&');
    }
    return {url};
  };

  const push = (params: RouteParams) => {
    const route = getRoute(params);

    uni.navigateTo(route);
  };
  const redirect = (params: RouteParams) => {
    const route = getRoute(params);

    uni.redirectTo(route);
  };
  const back = () => {
    uni.navigateBack();
  };

  const getPageQuery = () => {
    const pages = getCurrentPages();
    const currentPage: any = pages[pages.length - 1];
    const options = currentPage.options;
    return options;
  };

  return {push, redirect, back, getPageQuery};
}
